/**
*      author: Y_Yao
*      created: 2025-03-19 19:01:05
**/

#include<bits/stdc++.h>

#define int LL
using namespace std;
using LL = long long;
const LL LLINF = 9e18;
const int INF = 0x3f3f3f3f;
const int N = 1.5e5 + 7;
const int M = 3e5 + 7;
int n1,n2;
int m;
int dis[M],vis[M];
vector<vector<int>> edge(M);
struct node
{
    int dis,u;
    bool operator>(const node& t) const
    {
        return dis > t.dis;
    }
};
priority_queue<node,vector<node>,greater<node>> q;
void dijkstra(int s)
{
    memset(dis,INF,sizeof dis);
    memset(vis,false,sizeof vis);
    q.push({0,s});
    dis[s] = 0;
    while(!q.empty())
    {
        auto t = q.top();
        q.pop();
        if(vis[t.u]) continue;
        vis[t.u] = true;
        for(auto& e : edge[t.u])
        {
            if(dis[e] > dis[t.u] + 1)
            {
                dis[e] = dis[t.u] + 1;
            }
            q.push({dis[e],e});
        }
    }
}

void solve()
{
    cin >> n1 >> n2;
    cin >> m;
    for(int i = 1;i <= m;i++)
    {
        int u,v;
        cin >> u >> v;
        edge[u].push_back(v);
        edge[v].push_back(u);
    }
    dijkstra(1);
    int d1 = 0;
    for(int i = 1;i <= n1;i++)
    {
        d1 = max(d1,dis[i]);
    }
    dijkstra(n1 + n2);
    int d2 = 0;
    for(int i = n1 + 1;i <= n1 + n2;i++)
    {
        d2 = max(d2,dis[i]);
    }
    cout << d1 + d2 + 1 << endl;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);

    int t = 1;
    //cin >> t;
    while(t--)
    {
        solve();
    }
    return 0;
}